package com.shanxin.boom.mall.web.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shanxin.boom.mall.web.util.ApiMappingUtil;

public class ApiHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private static Logger logger = LoggerFactory.getLogger(ApiHttpServletRequestWrapper.class);

    public ApiHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getRequestURI() {
        return super.getRequestURI();
    }

    @Override
    public StringBuffer getRequestURL() {
        return super.getRequestURL();
    }

    @Override
    public String getServletPath() {
        String originPath = super.getServletPath();
        if ("/api".equals(originPath)) {
            String apiMethod = getParameter("apiMethod");
            String apiUrl = ApiMappingUtil.getMappingUrl(apiMethod);
            if (StringUtils.isNotBlank(apiUrl)) {
                return apiUrl;
            } else {
                logger.error("api请求{}未查询到对应的请求url.", apiMethod);
                return originPath;
            }
        } else {
            return originPath;
        }
    }

}
